﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Core.Library;
using DataObject.Category;

namespace DataObject.Product
{
    public class ProductController
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static void Product_Create(ProductObject o)
        {
            ProductDAL.Product_Create(o);
        }

        public static void Product_Update(ProductObject o)
        {
            ProductDAL.Product_Update(o);
        }

        public static void Product_Update_Status(int id, int status)
        {
            ProductDAL.Product_Update_Status(id, status);
        }

        public static void Product_Update_ImagePath(int id, string imgPath)
        {
            ProductDAL.Product_Update_ImagePath(id, imgPath);
        }

        public static void Product_Delete(int productId)
        {
            ProductDAL.Product_Delete(productId);
        }

        public static ProductObject Product_GetById(int productId)
        {
            return ObjectHelper.FillObject<ProductObject>(ProductDAL.Product_GetById(productId));
        }

        public static string GetProductName(int productId)
        {
            try { return Product_GetById(productId).Name; }
            catch { return string.Empty; }    
        }

        public static int Product_GetStatistic(int status)
        {
            return ProductDAL.Product_GetStatistic(status);
        }


        /***************************************
        = Front End
        -------------------------------------- */

        public static DataTable Product_ListTopHightlightProduct(int count)
        {
            return ProductDAL.Product_ListTopHightlightProduct(count);
        }

        public static DataTable Product_TopProductByCategory(int categoryId, int count)
        {
            return ProductDAL.Product_TopProductByCategory(categoryId, count);
        }

        public static DataTable Product_ListPaginationByCategory(string keyword, int categoryId, int status, int type, int pageIndex, int pageSize, ref int totalRecords)
        {
            return ProductDAL.Product_ListPaginationByCategory(keyword, categoryId, status, type, pageIndex, pageSize, ref totalRecords);
        }

        public static DataTable Product_ListPaginationRelatedProduct(int count, int categoryId, int productId)
        {
            return ProductDAL.Product_ListPaginationRelatedProduct(count, categoryId, productId);
        }

        public static DataTable Product_ListTopDiscount(int count)
        {
            return ProductDAL.Product_ListTopDiscount(count);            
        }

        public static DataTable Product_ListPaginationSearch(int categoryId, int pageIndex, int pageSize)
        {
            return ProductDAL.Product_ListPaginationSearch(categoryId, pageIndex, pageSize);
        }

        public static int Product_ListPaginationSearchCount(int categoryId)
        {
            return ProductDAL.Product_ListPaginationSearchCount(categoryId);
        }

        public static DataTable Product_ListPagination_Filter(string categoryId, string propertyValueId, 
                                                                string orderBy, int pageIndex, int pageSize, ref int totalRecords)
        {
            return ProductDAL.Product_ListPagination_Filter(categoryId, propertyValueId, orderBy, pageIndex, pageSize, ref totalRecords);
        }

        public static string Product_ListIdCategory(int categoryId)
        {
            var categories = string.Format("{0}, ", categoryId.ToString());

            var lstCategory = new List<CategoryObject>();
            CategoryController.GetListTree(lstCategory, (int)CategoryType.Product, 0, categoryId);
            categories = lstCategory.Count > 0
                            ? lstCategory.Aggregate(categories, (current, categoryObject) => current + string.Format("{0}, ", categoryObject.CategoryId))
                            : string.Format("{0}, ", categoryId);

            if (!string.IsNullOrEmpty(categories))
                categories = categories.Substring(0, categories.Length - 2);

            return categories;
        }
    }
}
